<!-- APP版本管理 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="平台" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择平台" clearable size="small">
          <el-option
            v-for="item in typeOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="版本" prop="version">
        <el-input
          v-model="queryParams.version"
          placeholder="请输入版本"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="版本号" prop="versionNo">
        <el-input
          v-model="queryParams.versionNo"
          placeholder="请输入版本号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否必须更新" prop="isMust">
        <el-select v-model="queryParams.isMust" placeholder="请选择是否必须更新" clearable size="small">
          <el-option
            v-for="item in isMustOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tws:version:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tws:version:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tws:version:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tws:version:export']"
        >导出</el-button>
      </el-col> -->
    </el-row>

    <el-table v-loading="loading" :data="versionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="平台" align="center" prop="type" />
      <el-table-column label="文件下载地址" align="center" prop="fileUrl" />
      <el-table-column label="版本" align="center" prop="version" />
      <el-table-column label="版本号" align="center" prop="versionNo" />
      <el-table-column label="是否必须更新" align="center" prop="isMust" :formatter="isMustFormat" />
      <el-table-column label="升级信息" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column label="升级时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            style="color: #71e2a3;"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tws:version:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['tws:version:query']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改app版本管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="平台" prop="type">
          <el-radio-group v-model="form.type" :disabled="readOnly">
            <el-radio v-for="item in typeOptions" :key="item.dictValue" :label="item.dictLabel" :disabled="item.dictLabel == 'ios'"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="文件下载地址" prop="fileUrl">
          <el-input v-model="form.fileUrl" placeholder="请输入文件下载地址" :readonly="readOnly" />
        </el-form-item>
        <el-form-item label="版本" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本" :readonly="readOnly" />
        </el-form-item>
        <el-form-item label="版本号" prop="versionNo">
          <el-input v-model="form.versionNo" placeholder="请输入版本号" readonly />
        </el-form-item>
        <el-form-item label="是否必须更新" prop="isMust">
          <el-switch
            v-model="form.isMust"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
            active-text="是"
            inactive-text="否" :disabled="readOnly">
          </el-switch>
        </el-form-item>
        <el-form-item label="升级信息" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :readonly="readOnly" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :disabled="disabled" @click="submitForm" v-if="!readOnly">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVersion, getNewVersionCode, getVersion, delVersion, addVersion, updateVersion } from "@/api/tws/version";

export default {
  name: "Version",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 禁用弹窗新增或修改的确定按钮
      disabled: false,
      readOnly: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // app版本管理表格数据
      versionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      androidMaxCode: 0,  // android当前最新版本号
      iosMaxCode: 0,  // ios当前最新版本号
      typeOptions: [{
        dictValue: 'android',
        dictLabel: 'android'
      }, {
        dictValue: 'ios',
        dictLabel: 'ios'
      }],
      isMustOptions: [{
        dictValue: '0',
        dictLabel: '否'
      }, {
        dictValue: '1',
        dictLabel: '是'
      }],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        type: null,
        fileUrl: null,
        version: null,
        versionNo: null,
        isMust: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "类型不能为空", trigger: [ "blur", "change" ] }
        ],
        fileUrl: [
          { required: true, message: "下载地址不能为空", trigger: "blur" }
        ],
        version: [
          { required: true, message: "版本不能为空", trigger: "blur" }
        ],
        versionNo: [
          { required: true, message: "版本号不能为空", trigger: [ "blur", "change" ] }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getNewCode();
  },
  methods: {
    /** 查询app版本管理列表 */
    getList() {
      this.loading = true;
      listVersion(this.queryParams).then(response => {
        this.versionList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    /** 查询app最新版本号 */
    getNewCode() {
      getNewVersionCode().then(response => {
        this.androidMaxCode = response.data.android;
        this.iosMaxCode = response.data.ios;
      })
    },
    // 是否必须更新翻译
    isMustFormat(row, column) {
      return this.selectDictLabel(this.isMustOptions, row.isMust);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: 'android',
        fileUrl: null,
        version: null,
        versionNo: null,
        isMust: '0',
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
      this.readOnly = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加app版本管理";
      if(this.form.type == 'android') {
        this.form.versionNo = this.androidMaxCode + 1;
      } else if(this.form.type == 'ios') {
        this.form.versionNo = this.iosMaxCode + 1;
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVersion(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改app版本管理";
      });
    },
    // 详情
    handleView(row) {
      this.reset();
      const id = row.id
      getVersion(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.readOnly = true;
        this.title = "app版本详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.disabled = true;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVersion(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.disabled =false;
              this.open = false;
              this.getList();
              this.getNewCode();
            }).catch(err => {
              this.disabled =false;
            })
          } else {
            addVersion(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.disabled =false;
              this.open = false;
              this.getList();
              this.getNewCode();
            }).catch(err => {
              this.disabled =false;
            })
          }
        } else {
          this.disabled = false;
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除app版本管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delVersion(ids);
        }).then(() => {
          this.$modal.msgSuccess("删除成功");
          this.getList();
          this.getNewCode();
        }).catch(err => {
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tws/version/export', {
        ...this.queryParams
      }, `tws_version.xlsx`)
    }
  }
};
</script>